<?php
declare(strict_types=1);

namespace App\ZxAdmin\Users;

use App\Controller;
use Field\SysUserConst;
use Field\SysUserColumns;
use Field\SysUserRow;

// hook controller_zx_admin_users_user_use.php

/**
 * @title   用户
 * @auth    true
 * @login   true
 * @menu    true
 * @component   vlist/index.vue
 * @icon    el-icon-user
 * @active  true
 * @affix   false
 * @rank    99
 */
class User extends Controller
{
    // hook controller_zx_admin_users_user_public_start.php



    // hook controller_zx_admin_users_user_public_end.php

    /**
     * @title  下发配置
     * @rank    0
     */
    public function action_Conf_PUT()
    {
        // hook controller_zx_admin_users_user_conf_start.php
        $conf=['pageSize'=>'20','remoteSort'=>0,'remoteFilter'=>0,'remoteSummary'=>0,'hidePagination'=>0,'hideRefresh'=>1,'hideDo'=>0,'add'=>1,'start'=>0,'stop'=>0,'delete'=>1,'import'=>0,'export'=>0,'pageSizes'=>[10,20,25,50,100,500,1000,10000],'TreeData'=>0,'enable'=>1,'disable'=>1,'chartRange'=>0,'copy'=>1,'Local'=>0];
        $Columns = [['label'=>'操作','type'=>'button','options'=>[['do'=>'enable','type'=>'','value'=>'','Name'=>'启用','Do'=>'Custom','Type'=>'primary','apiObjRemote'=>'zxadmin/content/articles/add','width'=>'1000'],['do'=>'disable','type'=>'success','value'=>'','Name'=>'禁用','Do'=>'Custom','Type'=>'primary','apiObjRemote'=>'zxadmin/content/articles/add','width'=>'1000'],['do'=>'custom','type'=>'primary','value'=>'','Name'=>'自定','Do'=>'Custom','Type'=>'success','apiObjRemote'=>'zxadmin/content/cate/add','width'=>'1000'],['do'=>'delete','type'=>'danger','value'=>'','Name'=>'删除','Do'=>'Custom','Type'=>'danger','apiObjRemote'=>'zxadmin/content/cate/add','width'=>'1000']]],['table'=>'SysUser','prop'=>'UserID','label'=>'会员ID','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysUser','prop'=>'ParentUserID','label'=>'推荐用户ID','hidden'=>1,'search'=>0,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysUser','prop'=>'UnID','label'=>'用来对外显示','hidden'=>1,'search'=>0,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysUser','prop'=>'RoleID','label'=>'权限ID','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>1,'sortable'=>0,'batch'=>0,'editable'=>0,'type'=>'select'],['table'=>'SysUser','prop'=>'AvatarAt','label'=>'头像','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysUser','prop'=>'Mobile','label'=>'手机号','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0,'batch'=>0,'editable'=>0],['table'=>'SysUser','prop'=>'UserName','label'=>'账户','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysUser','prop'=>'NickName','label'=>'昵称','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysUser','prop'=>'Balance','label'=>'余额','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysUser','prop'=>'Bonus','label'=>'佣金','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysUser','prop'=>'Points','label'=>'积分','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysUser','prop'=>'CreateIP','label'=>'注册IP','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysUser','prop'=>'CreateAt','label'=>'创建时间','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysUser','prop'=>'UpdateAt','label'=>'更新时间','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysUser','prop'=>'LoginNum','label'=>'登陆次数','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0],['table'=>'SysUser','prop'=>'Status','label'=>'状态','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>1,'sortable'=>0,'batch'=>0,'editable'=>0,'type'=>'dict','dict'=>'On'],['table'=>'SysUser','prop'=>'UserLevelID','label'=>'代理等级','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>1,'sortable'=>0,'type'=>'select','batch'=>0,'editable'=>0],['table'=>'SysUser','prop'=>'ShopID','label'=>'淘宝店铺ID','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>1,'sortable'=>0,'batch'=>0,'editable'=>0,'type'=>'input']];
        $Filter = [['table'=>'SysUser','prop'=>'UserName','label'=>'账户','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'component'=>'input','mode'=>'LI','span'=>4],['table'=>'SysUser','prop'=>'Status','label'=>'状态','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'mode'=>'=','component'=>'radioGroup','dict'=>'On','span'=>4],['table'=>'SysUser','prop'=>'ShopID','label'=>'淘宝店铺ID','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'component'=>'input','mode'=>'=','span'=>4],['table'=>'SysUser','prop'=>'Mobile','label'=>'手机号','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'mode'=>'LI','component'=>'input','span'=>4],['table'=>'SysUser','prop'=>'UserLevelID','label'=>'代理等级','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'component'=>'input','mode'=>'=','span'=>4],['table'=>'SysUser','prop'=>'CreateAt','label'=>'创建时间','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'component'=>'date','type'=>'daterange','valueFormat'=>'YYYY-MM-DD','mode'=>'><','span'=>4],['table'=>'SysUser','prop'=>'NickName','label'=>'昵称','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'component'=>'input','mode'=>'LI','span'=>4],['table'=>'SysUser','prop'=>'RoleID','label'=>'权限ID','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'mode'=>'=','component'=>'select','multiple'=>1,'span'=>4],['table'=>'SysUser','prop'=>'UserID','label'=>'会员ID','hidden'=>0,'search'=>1,'add'=>0,'edit'=>0,'field'=>0,'sortable'=>0,'mode'=>'=','component'=>'input','span'=>4]];
        $Tools = [['do'=>'add','type'=>'success','value'=>'','Name'=>'一个','Do'=>'Custom','Type'=>'success','multiple'=>0,'width'=>'1000','Field'=>'ParentUserID'],['do'=>'add','type'=>'','value'=>'','Name'=>'二哥','Do'=>'Dict','Type'=>'danger','multiple'=>0,'width'=>'1000','Field'=>'RoleID'],['do'=>'add','type'=>'','value'=>'','Name'=>'三个','Do'=>'Dict','Type'=>'info','multiple'=>0,'width'=>'1000','Field'=>'UserLevelID']];

        // hook controller_zx_admin_users_user_conf_end.php
        $this->response('0000', ['Conf' =>$conf, 'Filter' => $Filter, 'Tools' =>$Tools, 'Columns' => $Columns]);
    }

    /**
     * @title   下发列表数据
     * @rank    73
     */
    public function action_Index_GET()
    {
        // hook controller_zx_admin_users_user_index_start.php
        $page = $this->request->param('page', 0);
        $size = $this->request->param('limit', 0);
        $field = $this->request->param('field', 'UserID');
        $sort = $this->request->param('sort', -1);
        $order = [];
        !empty($field) && $order[$field] = $sort == 'asc' ? -1 : 1;
        $cond = [];
        $UserName=$this->request->param('UserName',);
		!empty($UserName) && $cond['UserName']['LI']=$UserName;
		$Status=$this->request->param('Status',-1);
		$Status!=-1 && $cond['Status']=$Status;
		$ShopID=$this->request->param('ShopID',0);
		!empty($ShopID) && $cond['ShopID']=$ShopID;
		$Mobile=$this->request->param('Mobile',);
		!empty($Mobile) && $cond['Mobile']['LI']=$Mobile;
		$UserLevelID=$this->request->param('UserLevelID',0);
		!empty($UserLevelID) && $cond['UserLevelID']=$UserLevelID;
		$CreateAt=$this->request->param('CreateAt',[]);
		!empty($CreateAt) && $cond['CreateAt']['><']=[$CreateAt[0].' 00:00:00',$CreateAt[1].' 23:59:59'];
		$NickName=$this->request->param('NickName',);
		!empty($NickName) && $cond['NickName']['LI']=$NickName;
		$RoleID=$this->request->param('RoleID',0);
		!empty($RoleID) && $cond['RoleID']=$RoleID;
		$UserID=$this->request->param('UserID',0);
		!empty($UserID) && $cond['UserID']=$UserID;
        $data = $this->SysUser->GetList($cond, $order, $page, $size);
        
        foreach ($data['data'] as &$row) {
            
            //unset($row['PassWord'],$row['Salt']);
        }

        // hook controller_zx_admin_users_user_index_end.php
        $this->response('0000', $data);
    }
    
    /**
     * @title   新增配置
     * @rank    72
     */
    public function action_Add_PUT()
    {
        // hook controller_zx_admin_users_user_add_put_start.php
        $AddForm = [['table'=>'SysUser','prop'=>'ParentUserID','label'=>'推荐用户ID','hidden'=>1,'search'=>0,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0,'required'=>0,'validator'=>'^[0-9]*$','component'=>'select','multiple'=>0],['table'=>'SysUser','prop'=>'RoleID','label'=>'权限ID','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0,'required'=>1,'validator'=>'^[0-9]*$','component'=>'select','multiple'=>0],['table'=>'SysUser','prop'=>'Mobile','label'=>'手机号','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0,'required'=>0,'validator'=>'^(1[3-9]\\d{9}]$','component'=>'input','maxlength'=>32],['table'=>'SysUser','prop'=>'UserName','label'=>'账户','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0,'required'=>1,'validator'=>'^[\\u4E00-\\u9FA5A-Za-z0-9]+$','component'=>'input','maxlength'=>32],['table'=>'SysUser','prop'=>'NickName','label'=>'昵称','hidden'=>0,'search'=>1,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0,'required'=>0,'validator'=>'^[A-Za-z0-9]+$','component'=>'input','maxlength'=>32]];
        // hook controller_zx_admin_users_user_add_put_end.php
        $this->response('0000', ['data' => $AddForm]);
    }

    /**
     * @title   新增提交
     * @rank    71
     */
    public function action_Add_POST()
    {
        // hook controller_zx_admin_users_user_add_post_start.php
        $data = [];
        $data['ParentUserID']=$this->request->param('ParentUserID',0);
		!empty($data['ParentUserID']) && !preg_match('/^[0-9]*$/', $data['ParentUserID']) && $this->response('0003',[],'验证失败');
		$data['RoleID']=$this->request->param('RoleID',0);
		!preg_match('/^[0-9]*$/', $data['RoleID']) && $this->response('0003',[],'验证失败');
		$data['Mobile']=$this->request->param('Mobile',);
		!empty($data['Mobile']) && !preg_match('/^(1[3-9]\d{9})$/', $data['Mobile']) && $this->response('0003',[],'验证失败');
		$data['UserName']=$this->request->param('UserName',);
		!preg_match('/^[\u4E00-\u9FA5A-Za-z0-9]+$/', $data['UserName']) && $this->response('0003',[],'验证失败');
		$data['NickName']=$this->request->param('NickName',);
		!empty($data['NickName']) && !preg_match('/^[A-Za-z0-9]+$/', $data['NickName']) && $this->response('0003',[],'验证失败');
        $r = $this->SysUser->insert($data);
        // make addformpost SysUser ZxAdmin\Users
        // hook controller_zx_admin_users_user_add_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    
    
    
    /**
     * @title   编辑字段
     * @rank    60
     */
    public function action_Field_POST()
    {
        // hook controller_zx_admin_users_user_field_post_start.php
        $cond = [];
        $cond['UserID']=$this->request->param('UserID',0);
        $field = $this->request->param('field', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$field,...$cond], ['待修改数据','待修改字段']);
        $field_fields = ['RoleID','Status','UserLevelID','ShopID'];
        $tmp_field=rtrim($field,'+-*/=');
        !in_array($tmp_field, $field_fields, true) && $this->response('0001', [], '字段不允许修改');
        !in_array($tmp_field, SysUserConst::fields, true) && $this->response('0001', [], '字段不允许修改');
        $update = [];
        switch ($tmp_field) {
        case 'RoleID':
         case 'Status':
         case 'UserLevelID':
         case 'ShopID':
             $update[$field] = intval($value);
            break;
        }
        $r = !empty($update) ? $this->SysUser->update($cond, $update) : false;
        // hook controller_zx_admin_users_user_field_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    
    

    
    /**
     * @title   启用
     * @rank    41
     */
    public function action_Enable_POST()
    {
        // hook controller_zx_admin_users_user_enable_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
                isset($row['UserID']) && $cond['UserID'][]=intval($row['UserID']);
        }
        $this->CheckEmpty($cond, ['待修改数据']);
        $r = $this->SysUser->update($cond, [SysUserConst::Status => 1]);
        // hook controller_zx_admin_users_user_enable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }

    /**
     * @title   禁用
     * @rank    40
     */
    public function action_Disable_POST()
    {
        // hook controller_zx_admin_users_user_disable_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
                isset($row['UserID']) && $cond['UserID'][]=intval($row['UserID']);
        }
        $this->CheckEmpty($cond, ['操作数据为空']);
        $r = $this->SysUser->update($cond, [SysUserConst::Status => 0]);
        // hook controller_zx_admin_users_user_disable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    
    
    /**
     * @title   下拉选择属性
     * @rank    30
     */
    public function action_Option_GET()
    {
        // hook controller_zx_admin_users_user_option_start.php

        $value = $this->request->param('value');
        $cond = [
            'OptionField'=>['LIKE'=>$value]
        ];
        $order = [];
        $list = $this->SysUser->select($cond, $order, 1, 100);
        $data=[];
        foreach ($list as $row){
            $data[]=[
              'label'=>$row['OptionLabel'],
              'value'=>$row['OptionValue'],
            ];
        }
        // hook controller_zx_admin_users_user_option_end.php
        $this->response('0000', ['data' => $data]);
    }
    
    
    

      
     /**
      * @title   删除数据
      * @rank    50
      */
    public function action_Index_Delete()
    {
        // hook controller_zx_admin_users_user_delete_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
            isset($row['UserID']) && $cond['UserID'][]=intval($row['UserID']);
        }
        $this->CheckEmpty($cond, ['操作数据为空']);
        $r = $this->SysUser->delete($cond);
        // hook controller_zx_admin_users_user_delete_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }
    
    // hook controller_zx_admin_users_user_end.php
}

?>